package com.coocaa.ops.api.v1.internal;

import com.coocaa.ops.api.base.cache.properties.CacheProperties;
import com.coocaa.ops.api.v1.internal.service.ICacheInternalOpsService;
import com.coocaa.ops.api.v1.internal.service.ICacheUpdateMgrService;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.validator.MyValidators;
import com.coocaa.ops.common.core.biz.cache.model.query.DelKeyModel;
import com.coocaa.ops.common.core.biz.cache.model.query.UpdateValueModel;
import com.coocaa.ops.common.core.biz.cache.validator.DeleteCacheValidator;
import com.coocaa.ops.common.core.security.annotation.MyGuard;
import com.coocaa.ops.common.tool.regex.RegexUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-08-11
 */
@RestController
@RequestMapping("/tvos/internal")
public class InternalController {

    @Autowired
    private ICacheUpdateMgrService cacheUpdateMgrService;
    @Autowired
    private ICacheInternalOpsService cacheInternalOpsService;

    @MyGuard
    @RequestMapping("/updateCache")
    public R updateCache(HttpServletRequest request, UpdateValueModel model) {
        if (CacheProperties.Constants.ENABLE_CACHE) {
            MyValidators.validIdAndType(model.getId(), model.getType());
            List<String> data = cacheUpdateMgrService.updateCacheByResourceIdAndType(model.getId(), model.getType());
            return R.ok(data);
        }
        return R.ok();
    }

    @RequestMapping("/listKeyType")
    public R listKeyType(HttpServletRequest request) {
        return cacheInternalOpsService.listKeyType();
    }


    @RequestMapping("/listKey")
    public R listKey(HttpServletRequest request,
                     @RequestParam String key,
                     @RequestParam String cacheEnv,
                     @RequestParam Integer page,
                     @RequestParam(required = false) Integer pageSize,
                     @RequestParam(required = false) Integer scanCount,
                     @RequestParam(required = false) Integer match) {
        if (StringUtils.isBlank(key)) {
            return R.fail("key 不能为空");
        }
        if (RegexUtil.matchChinese(key)) {
            return R.fail("key 不能为中文");
        }
        if (scanCount == null) {
            return cacheInternalOpsService.listKey(key, cacheEnv, page, pageSize, match);
        } else {
            return cacheInternalOpsService.listKeyV2(key, cacheEnv, scanCount, pageSize, match);
        }
    }

    @RequestMapping("/getValByKey")
    public R getValByKey(HttpServletRequest request, @RequestParam String key,@RequestParam(required = false) String cacheEnv) {
        if (StringUtils.isBlank(key)) {
            return R.fail("key 不能为空");
        }
        return cacheInternalOpsService.getValByKey(key,cacheEnv);
    }

    @MyGuard
    @RequestMapping(value = "/delByKey")
    public R delByKey(HttpServletRequest request, DelKeyModel model) {
        if (ArrayUtils.isEmpty(model.getKeys())) {
            return R.fail("keys 不能为空");
        }
        if (DeleteCacheValidator.isDisablePatternDel(model.getMatch(), request)){
            return R.fail("当前模糊删除操作 不允许");
        }
        return cacheInternalOpsService.delByKey(model.getKeys(), model.getMatch(), model.getCacheEnv());
    }

    @RequestMapping("/ttl")
    public R ttl(HttpServletRequest request, @RequestParam String key,@RequestParam(required = false) String cacheEnv) {
        if (StringUtils.isEmpty(key)) {
            return R.fail("key 不能为空");
        }
        return cacheInternalOpsService.ttl(key,cacheEnv);
    }



    /*************** ===私有方法=== ********************* ===start=== ***************************************/

}
